﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace BillingManagment
{
    public partial class AddItem : System.Web.UI.Page
    {
        DbConnect db;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFarmerList();
                txtDate.Text = DateTime.Now.ToShortDateString();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ddlFarmer.SelectedValue == "0" || txtDate.Text == "" || txtNoOfBucket.Text == "")
            {
                Page.RegisterStartupScript("alert", "<script>alert('Fill Required Field');</script>");
            }
            else
            {
                db = new DbConnect();
                string Total = "";
                SqlCommand cmd = new SqlCommand("Insert into tbl_Inward (Farmer_ID,NoOfBucket,Date,TotalBucket)values('" + ddlFarmer.SelectedValue + "','" + txtNoOfBucket.Text + "',Convert(datetime,'" + txtDate.Text + "',103),'" + txtReturnBucket.Text + "')");
                db.ExecuteQuery(cmd.CommandText, CommandType.Text);

                db = new DbConnect();
                SqlCommand cmd1 = new SqlCommand("Select top 1 Total_Bucket from tbl_Inventory_History where Farmer_ID='" + ddlFarmer.SelectedValue + "' order by Modified_Date Desc");
                DataTable dt1 = new DataTable();
                dt1 = db.GetDataTable(cmd1.CommandText, CommandType.Text);
                if (dt1.Rows.Count > 0)
                {
                    Total = dt1.Rows[0][0].ToString();
                    Total = Convert.ToString(Convert.ToInt32(Total) + Convert.ToInt32(txtNoOfBucket.Text) - Convert.ToInt32(txtReturnBucket.Text));
                }
                else
                {
                    Total = "0";
                    Total = Convert.ToString(Convert.ToInt32(Total) + Convert.ToInt32(txtNoOfBucket.Text) - Convert.ToInt32(txtReturnBucket.Text));
                }

                db = new DbConnect();
                SqlCommand cmd2 = new SqlCommand("Insert into tbl_Inventory_History (Farmer_ID,Modified_Date,Received_Bucket,Sold_Bucket,Return_Bucket,Total_Bucket)values('" + ddlFarmer.SelectedValue + "',Convert(datetime,'" + txtDate.Text + "',103),'" + txtNoOfBucket.Text + "','0','" + txtReturnBucket.Text + "','" + Total + "')");
                db.ExecuteQuery(cmd2.CommandText, CommandType.Text);

                ClearControls();
                Page.RegisterStartupScript("alert", "<script>alert('Data Enter Successfully.');</script>");
            }
        }

        public void BindFarmerList()
        {
            db = new DbConnect();
            SqlCommand cmd = new SqlCommand("select ID, (Name +' '+ village) as FName from tbl_Farmer");
            DataTable dt = new DataTable();
            dt = db.GetDataTable(cmd.CommandText, CommandType.Text);
            ddlFarmer.DataSource = dt;
            ddlFarmer.DataValueField = "ID";
            ddlFarmer.DataTextField = "FName";
            ddlFarmer.DataBind();
            ddlFarmer.Items.Insert(0, "<----Select---->");
        }

        public void ClearControls()
        {
            txtDate.Text = "";
            txtNoOfBucket.Text = "";
            txtReturnBucket.Text = "";
            //  ddlFarmer.SelectedValue = "0";
        }
    }
}